package com.zxt.web.controller;

import com.zxt.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * projectName:myXiaoMi
 * author:ZhangXiaoTeng
 * time:2021/12/8 22:12
 * description:
 */
@WebServlet("/goodsImgs")
public class GoodsImgsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String pic = request.getParameter("pic");
        if(StringUtils.isEmpty(pic)){
            return;
        }
        String basePath = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images" + File.separator + pic);
        File file=new File(basePath);
        if(file.exists()){
            FileInputStream fis=new FileInputStream(file);
            ServletOutputStream os = response.getOutputStream();
            byte [] buf=new byte[1024*8];
            int len;
            while((len=fis.read(buf))!=-1){
                os.write(buf,0,len);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

